<template>
    <div>
        <div class="permissManaTool">
            <el-input size="small" placeholder="请输入英文名称" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" v-model="role.nameZh" placeholder="请输入中文名称"></el-input>
            <el-button size="small" icon="el-icon-circle-plus" type="primary" @click="addRole">添加角色</el-button>
        </div>
        <div style="margin-top: 8px;width: 58%">
            <el-collapse v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问页面</span>
                            <el-button style="float: right; padding: 3px 0; color: #ff1a29"
                                       @click="deleteRole(r)" type="text" icon="el-icon-delete">删除角色</el-button>
                        </div>
                        <div>
                            <el-tree  node-key="id"
                                      ref="tree"
                                      :key="index"
                                      :default-checked-keys="checkedMenus" show-checkbox :data="allMenus" :props="defaultProps"></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button @click="cancleUpdate">取消修改</el-button>
                                <el-button type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data(){
            return{
                activeName:-1,
                checkedMenus:[

                ],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                role:{
                    name:"",
                    nameZh:""
                },
                roles:[

                ],
                allMenus:[

                ]
            }
        },
        mounted() {
            this.initRoles();
        },
        methods:{
            deleteRole(role){
                this.$confirm('此操作将删除【'+role.nameZh+'】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/premiss/"+role.id).then(resp=>{
                        if(resp){
                            this.initRoles();
                            this.activeName=-1;
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addRole(){
                if(this.role.name && this.role.nameZh){
                    this.postRequest("/system/basic/premiss/",this.role).then(resp=>{
                        if(resp){
                            this.role.name="";
                            this.role.nameZh="";
                            this.initRoles();
                        }
                    })
                }else {
                    this.$message.error("请补充字段！");
                }
            },
            cancleUpdate(){
                this.activeName = -1;
            },
            doUpdate(rid,index){
                let tree = this.$refs.tree[index];
                let selectKeys = tree.getCheckedKeys(true);
                console.log(selectKeys);
                let url = "/system/basic/premiss/?rid="+rid;
                selectKeys.forEach(key=>{
                    url += "&mids=" + key;
                })
                if(selectKeys.length !== 0){
                    this.putRequest(url).then(resp=>{
                        if(resp){
                            this.activeName = -1;
                        }
                    })
                }else{
                    this.$message.error("请先给角色设置访问页面");
                }
            },
            change(rid){
                if(rid){
                    this.initAllMenus();
                    this.initCheckedMenus(rid);
                }
            },
            initCheckedMenus(rid){
                this.getRequest("/system/basic/premiss/mids/"+rid).then(resp=>{
                    if(resp){
                        this.checkedMenus = resp;
                    }
                })
            },
            initAllMenus(){
                this.getRequest("/system/basic/premiss/menus").then(resp=>{
                    if(resp){
                        this.allMenus = resp;
                    }
                })
            },
            initRoles(){
                this.getRequest("/system/basic/premiss/").then(resp=>{
                    if(resp){
                        this.roles = resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .permissManaTool {
        display: flex;
        justify-content: flex-start;
    }
    .permissManaTool .el-input {
        width: 300px;
        margin-right: 6px;
    }
</style>